/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.moravian.UI;

import edu.moravian.grid.Grid;
import edu.moravian.managers.EnemyManager;
import edu.moravian.managers.OrdinanceManager;
import edu.moravian.managers.TowerManager;
import edu.moravian.managers.WaveGenerator;
import edu.moravian.math.Point2D;
import edu.moravian.path.PathGenerator;
import edu.moravian.player.PlayerInfo;
import edu.moravian.world.World2D;
import edu.moravian.world.WorldDescriptor;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ronaghan
 */
public class GameViewManager extends GameView
{
    private List<GameView> views;

    private WorldDescriptor playAreaWD;
    private WorldDescriptor gameInfoWD;
    private WorldDescriptor towerSelectionWD;
    private WorldDescriptor towerUpgradeWD;
    private WorldDescriptor sendWaveWD;

    private TowerUpgradeView towerUpgradeView;
    private PlayAreaView playAreaView;  
    private GameInfoView gameInfoView;     
    private TowerSelectionView towerSelectionView;
    private SendWaveView sendWaveView;
             
    public GameViewManager(WorldDescriptor world, OrdinanceManager ordinanceManager, TowerManager towerManager, 
            EnemyManager enemyManager, Grid playAreaGrid, PlayerInfo playerInfo, WorldDescriptor playAreaWD, WaveGenerator waveGenerator)
    {
        super(world);
        
        this.playAreaWD = playAreaWD;
        setupViewBounds(world);
        
        
        towerUpgradeView = new TowerUpgradeView(towerUpgradeWD, playAreaView, towerManager);
        playAreaView = new PlayAreaView(ordinanceManager, towerManager, enemyManager, playAreaWD, playAreaGrid, towerUpgradeView);
        gameInfoView = new GameInfoView(gameInfoWD, playerInfo);
        towerSelectionView = new TowerSelectionView(towerSelectionWD, playAreaView, towerUpgradeView);
        sendWaveView = new SendWaveView(sendWaveWD, waveGenerator, enemyManager);
        
        towerUpgradeView.setPlayAreaView(playAreaView);
        
        playAreaView.setPath(new PathGenerator(playAreaWD, playAreaGrid).getPath());
        
        views = new ArrayList<GameView>();
        views.add(towerUpgradeView);
        views.add(playAreaView);
        views.add(gameInfoView);
        views.add(towerSelectionView);
        views.add(sendWaveView);

    }
    
    @Override
    public void draw(World2D w2d)
    {
        w2d.drawCenteredImage(ImageManager.getBackground(), boundsDescriptor.getCenter(), boundsDescriptor.getWidth(), boundsDescriptor.getHeight());

        for(GameView view: views)
        {
            view.draw(w2d);
        }
    }




    @Override
    public void handleMouseClick(Point2D location)
    {
        if(towerSelectionView.inBounds(location) == false)
        {
            towerUpgradeView.setCurPlaceTower(null);
        }

        for(GameView view: views)
        {
            if(view.inBounds(location))
            {
                view.handleMouseClick(location);
            }
        }
    }




    @Override
    public void handleMouseMoved(Point2D location)
    {
        if(towerSelectionView.inBounds(location) == false)
        {
            towerUpgradeView.setCurPlaceTower(null);
        }
        
        for(GameView view: views)
        {
            if(view.inBounds(location))
            {
                view.handleMouseMoved(location);
            }
        }
    }
    
    
     private void setupViewBounds(WorldDescriptor world)
    {
        
        
        double gameInfoMinY = world.getMaxY()*.8;
        gameInfoWD = new WorldDescriptor(new Point2D(playAreaWD.getMaxX(), gameInfoMinY), world.getUpperRight());
        
        double towerMinY = world.getMaxY()*.5;
        towerSelectionWD = new WorldDescriptor(new Point2D(playAreaWD.getMaxX(), towerMinY), gameInfoWD.getLowerRight());
        
        
        double upgradeSendY = world.getMaxY()*.05;
        
        towerUpgradeWD = new WorldDescriptor(new Point2D(playAreaWD.getMaxX(), upgradeSendY), towerSelectionWD.getLowerRight());
        
        Point2D sendWaveUpperRight = new Point2D(world.getMaxX(), upgradeSendY);
        sendWaveWD = new WorldDescriptor(playAreaWD.getLowerRight(), sendWaveUpperRight);
    }
    
}



